<template>
    <view class="cart-container">
        <!--收货地址区域-->
        <my-address></my-address>
        <!--购物车标题区域-->
        <view class="cart-title">
            <uni-icons type="shop" size="18"></uni-icons>
            <text>购物车</text>
        </view>
        <!--购物车商品信息区域-->
        <uni-swipe-action v-if="cartList.length !== 0">
            <uni-swipe-action-item :right-options="options" class="cart-goods-info" v-for="(goods,i) in cartList"
                                   :key="i" @click="bindClick(goods.goods_id)">
                <my-goods :goods="goods" :show-radio="true" @radio-change="radioChangeHandler"
                          @number-change="numberChangeHandler"></my-goods>
            </uni-swipe-action-item>
        </uni-swipe-action>
        <!--自定义结算组件-->
        <my-settle v-if="cartList.length !== 0"></my-settle>
        <!--商品为空时展示区域-->
        <view class="empty" v-else>
            <image src="/static/empty.png"></image>
            <text>您的购物车还是空的，赶紧行动吧！</text>
            <text class="go" @click="gotoHome">去逛逛~</text>
        </view>
    </view>
</template>

<script>
    import tabBarBadge from "@/mixins/tabBar-badge.js"
    import {mapState, mapMutations} from "vuex"

    export default {
        mixins: [tabBarBadge],
        computed: {
            ...mapState('cart', ['cartList'])
        },
        data() {
            return {
                options: [
                    {
                        text: '删除',
                        style: {
                            backgroundColor: '#ff93a7'
                        }
                    }
                ]
            }
        },
        methods: {
            ...mapMutations('cart', ['updateGoodsState', 'updateGoodsCount', 'deleteGoodsById']),
            // 商品复选框状态改变处理事件
            radioChangeHandler(e) {
                this.updateGoodsState(e)
            },
            // 商品数量改变事件
            numberChangeHandler(e) {
                this.updateGoodsCount(e)
            },
            // 商品删除事件
            bindClick(goods_id) {
                this.deleteGoodsById(goods_id)
            },
            // 去逛逛处理事件
            gotoHome() {
                uni.switchTab({
                    url: '/pages/home/home'
                })
            }
        },

    }
</script>

<style lang="scss">
    .cart-container {
        padding-bottom: 50px;
        overflow-x: hidden;
    }

    .cart-title {
        height: 50px;
        display: flex;
        align-items: center;
        padding-left: 5px;
        border-bottom: 1px solid #efefef;

        text {
            font-size: 14px;
            margin-left: 5px;
        }
    }

    .empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
        height: 200px;
        margin-top: 80px;

        image {
            width: 100px;
            height: 100px;
        }

        text {
            font-size: 18px;
            margin-top: 15px;
            color: #ff93a7;
        }

        .go {
            border: 1px solid #f56c6c;
            padding: 3px 25px;
            border-radius: 5px;
        }
    }
</style>
